﻿using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Web.UI.WebControls;
using System.Data;

namespace MII_FIF.Layouts.MII_FIF.Master.SystemType
{
    public partial class ViewSystemType : LayoutsPageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Generic.IsUserInGroup(Configuration.MasterGroupName))
            {
                btnAdd.Click += btnAdd_Click;
                btnDelete.Click += btnDelete_Click;
                GridSystemType.RowCommand += GridSystemType_RowCommand;
                GridSystemType.PageIndexChanging += GridSystemType_PageIndexChanging;
                //((Label)Header.FindControl("lblHeader")).Text = "View System Type";
                if (!IsPostBack)
                {
                    load();
                }
            }
            else
            {
                Response.Redirect(Generic.GenLink("/"));
            }
        }

        void GridSystemType_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridSystemType.DataSource = ViewState["DTSystemType"];
            GridSystemType.PageIndex = e.NewPageIndex;
            GridSystemType.DataBind();
        }

        void GridSystemType_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName.ToLower() == "edit")
            {
                Response.Redirect(Generic.GenLink("/_Layouts/MII_FIF/Master/SystemType/AddSystemType.aspx?ID=" + e.CommandArgument));
            }
        }

        void btnDelete_Click(object sender, EventArgs e)
        {
            lblError.Text = string.Empty;
            bool delete = false;
            DataTable DTSystemType = (DataTable)ViewState["DTSystemType"];
            InventoryManagement.System_Type IM = new InventoryManagement.System_Type();
            int stat = 0;
            foreach (GridViewRow row in GridSystemType.Rows)
            {
                CheckBox chk = (CheckBox)row.FindControl("chk");
                if (chk != null)
                    if (chk.Checked)
                    {
                        delete = true;
                        break;
                    }
            }

            if (delete)
            {
                for (int i = GridSystemType.Rows.Count - 1; i > -1; i--)
                {
                    CheckBox chk = (CheckBox)GridSystemType.Rows[i].FindControl("chk");
                    if (chk != null)
                        if (chk.Checked)
                        {
                            DTSystemType.Rows.RemoveAt(i);
                            IM.ID = Guid.Parse(GridSystemType.Rows[i].Cells[3].Text.ToString());
                            IM.Title = GridSystemType.Rows[i].Cells[1].Text.ToString();
                            IM.Size = int.Parse(GridSystemType.Rows[i].Cells[2].Text.ToString());
                            IM.IsDeleted = true;
                            stat = IM.Update();
                            if (stat < 0)
                            {
                                lblError.Text += "Delete Failed"; break;
                            }
                        }
                }
                GridSystemType.DataSource = DTSystemType;
                GridSystemType.DataBind();
                ViewState["DTSystemType"] = DTSystemType;
            }
            else
            {
                lblError.Text += "Please check at least 1 item to delete <br />";
            }
        }

        void btnAdd_Click(object sender, EventArgs e)
        {
            Response.Redirect(Generic.GenLink("/_Layouts/MII_FIF/Master/SystemType/AddSystemType.aspx"));
        }
        void load()
        {
            InventoryManagement.System_Type IM = new InventoryManagement.System_Type();
            IM.IsDeleted = false;
            ViewState["DTSystemType"] = IM.SelectByParam();
            GridSystemType.DataSource = ViewState["DTSystemType"];
            GridSystemType.DataBind();
        }
    }
}
